<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import { Plus, Delete } from '@element-plus/icons-vue'
import { type Role, type AddRoleForm, getRoleList, addRole, delRole } from '@/api/sys'
import dayjs from 'dayjs'

defineOptions({
  name:"Role"
})

const tableData = ref<Role[]>([])
const isAddDialog = ref(true) // 是否为添加模式
const showDialog = ref(false) // 是否显示模态框
const dialogForm = reactive<AddRoleForm>({})

onMounted(() => {
  clickSearchBtn()
})

const clickSearchBtn = () => {
  getRoleList().then((res) => {
    tableData.value = res
  })
}
const clickAddBtn = () => {
  isAddDialog.value = true
  showDialog.value = true
  dialogForm.isOpen = 1
}
const clickSubDialogForm = () => {
  addRole({
    roleName: dialogForm.roleName,
    roleCode: dialogForm.roleCode,
    roleDesc: dialogForm.roleDesc,
    isOpen: dialogForm.isOpen,
  }).then(() => {
    clickSearchBtn()
    showDialog.value = false
  })
}
const clickDelBtn = (id: string) => {
  delRole(id).then(() => {
    clickSearchBtn()
  })
}
</script>

<template>
  <div>
    <el-dialog
      v-model="showDialog"
      :title="isAddDialog ? '添加角色信息' : '更新角色信息'"
      width="500"
    >
      <el-form :model="dialogForm" label-width="auto" style="max-width: 600px">
        <el-form-item label="名称">
          <el-input v-model="dialogForm.roleName" />
        </el-form-item>
        <el-form-item label="编码">
          <el-input v-model="dialogForm.roleCode" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="dialogForm.roleDesc" />
        </el-form-item>
        <el-form-item label="公开">
          <el-radio-group v-model="dialogForm.isOpen">
            <el-radio :value="1">公开</el-radio>
            <el-radio :value="2">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="clickSubDialogForm"> 提交 </el-button>
        </div>
      </template>
    </el-dialog>
    <el-row>
      <el-col>
        <el-card>
          <el-row class="mb-5">
            <el-col>
              <el-button type="primary" :icon="Plus" @click="clickAddBtn">添加角色</el-button>
            </el-col>
          </el-row>
          <el-table :data="tableData" stripe show-overflow-tooltip>
            <el-table-column type="index" width="50" label="#" />
            <el-table-column prop="roleName" label="名称" min-width="120" />
            <el-table-column prop="roleDesc" label="描述" min-width="120" />
            <el-table-column prop="roleCode" label="角色码" min-width="120" />
            <el-table-column prop="isSys" label="系统数据" min-width="120">
              <template #default="scope">
                {{ scope.row.isSys == 1 ? '是' : '否' }}
              </template>
            </el-table-column>
            <el-table-column prop="isOpen" label="是否公开" min-width="120">
              <template #default="scope">
                {{ scope.row.isOpen == 1 ? '是' : '否' }}
              </template>
            </el-table-column>
            <el-table-column prop="creator" label="创建人" min-width="120"> </el-table-column>
            <el-table-column prop="isOpen" label="创建时间" min-width="120">
              <template #default="scope">
                {{ dayjs(new Date(scope.row.createdAt * 1000)).format('YYYY-MM-DD HH:mm:ss') }}
              </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" min-width="180">
              <template #default="scope">
                <el-link
                  :icon="Delete"
                  class="mr-3"
                  type="primary"
                  @click="clickDelBtn(scope.row.roleId)"
                  >删除</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped></style>
